﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Tools.DataAccess.Repository;

namespace Tools.DataAccess.Common
{
    public abstract class AppDABase//<TContext> //: IAppDA
    //where TContext : IDbContext, new()
    {
        //public static IAppDA _AppDA;
        public static string ConnectionString
        {
            get
            {
                //return ConfigurationManager.AppSettings["MyAppConnectionString"];
                //return CommonInfo.GetConnectionString().Decrypt();
                return CommonInfo.GetConnectionString();
            }

        }

        //private static IDictionary<Type, object> _UnitOfWorkList = new Dictionary<Type, object>();

        //public static IUnitOfWork GetUnitOfWork<TContext>()
        //    where TContext : ContextBase, new()
        //{
        //    var type = typeof(TContext);
        //    if (!_UnitOfWorkList.ContainsKey(type))
        //        _UnitOfWorkList.Add(type, new UnitOfWork<TContext>());
        //    return (IUnitOfWork)_UnitOfWorkList[type];
        //}

        private static IUnitOfWork _UnitOfWork;

        public static IUnitOfWork GetUnitOfWork()
        {
            if (_UnitOfWork == null)
                _UnitOfWork = new UnitOfWork(ConnectionString);
            return _UnitOfWork;
            //var key = "MyUnitOfWork_" + HttpContext.Current.GetHashCode().ToString("x")
            //          + Thread.CurrentContext.ContextID.ToString();
            //IUnitOfWork unitOfWork = HttpContext.Current.Items[key] as IUnitOfWork;

            //if (unitOfWork == null)
            //{
            //    unitOfWork = new UnitOfWork(ConnectionString);
            //    HttpContext.Current.Items[key] = unitOfWork;
            //}
            //return unitOfWork;
        }

    }
}
